-- Title: Repulator
-- Version: 1.0
-- Revision Date: September 28, 2008
-- Author: Jeff "Taffu" Fancher
-- Notes: GPLv3 License
----------------------------------------------

local colorID = {
    [0] = "|cffffffff", -- Unknown
    [1] = "|cffff0000", -- Hated
    [2] = "|cffff8c00", -- Hostile
    [3] = "|cffff4500", -- Unfriendly
    [4] = "|cffffffff", -- Neutral
    [5] = "|cff008000", -- Friendly
    [6] = "|cff00ff7f", -- Honored
    [7] = "|cffffff00", -- Revered
    [8] = "|cffffd700", -- Exhaulted
}

local RepUpdate = function()
	local numFactions = GetNumFactions();
	local factionIndex, factionName, factionCheck, factionStanding, factionBar, factionHeader, color, tooltipStanding;
	local name, description, standingID, barMin, barMax, barValue, barNeeded, atWarWith, canToggleAtWar, isHeader, isCollapsed, isWatched;
	local atWarIndicator, rightBarTexture;
 
	-- Update scroll frame
	if ( not FauxScrollFrame_Update(ReputationListScrollFrame, numFactions, NUM_FACTIONS_DISPLAYED, REPUTATIONFRAME_FACTIONHEIGHT ) ) then
		ReputationListScrollFrameScrollBar:SetValue(0);
	end
	local factionOffset = FauxScrollFrame_GetOffset(ReputationListScrollFrame);
 
	local gender = UnitSex("player");
	for i=1, NUM_FACTIONS_DISPLAYED, 1 do
		factionIndex = factionOffset + i;
		factionBar = getglobal("ReputationBar"..i);
		factionHeader = getglobal("ReputationHeader"..i);
		factionCheck = getglobal("ReputationBar"..i.."Check");
		if ( factionIndex <= numFactions ) then
			name, description, standingID, barMin, barMax, barValue, atWarWith, canToggleAtWar, isHeader, isCollapsed, isWatched = GetFactionInfo(factionIndex);
			if ( isHeader ) then
				factionHeader:SetText("|cffffffff"..name.."|r");
				if ( isCollapsed ) then
					factionHeader:SetNormalTexture("Interface\\Buttons\\UI-PlusButton-Up");
				else
					factionHeader:SetNormalTexture("Interface\\Buttons\\UI-MinusButton-Up");
				end
				factionHeader.index = factionIndex;
				factionHeader.isCollapsed = isCollapsed;
				factionBar:Hide();
				factionHeader:Show();
				factionCheck:Hide();
			else
				factionStanding = GetText("FACTION_STANDING_LABEL"..standingID, gender);
				factionName = getglobal("ReputationBar"..i.."FactionName");
                name = format("%s%s|r", colorID[standingID], name)
                factionName:SetText(name)
				getglobal("ReputationBar"..i.."FactionStanding"):SetText(factionStanding);
 
				atWarIndicator = getglobal("ReputationBar"..i.."AtWarCheck");
				rightBarTexture = getglobal("ReputationBar"..i.."ReputationBarRight");
 
				if ( atWarWith ) then
					atWarIndicator:Show();
				else
					atWarIndicator:Hide();
				end
 
				-- Normalize values
				barMax = barMax - barMin;
				barValue = barValue - barMin;
                barNeeded = barMax - barValue;
				barMin = 0;
 
				factionBar.id = factionIndex;
				factionBar.standingText = factionStanding;
                if ( barMax == 1000 and barValue == 999 ) then
                    factionBar.tooltip = HIGHLIGHT_FONT_COLOR_CODE.." "..barValue.." / "..barMax..FONT_COLOR_CODE_CLOSE;
                else
                    factionBar.tooltip = HIGHLIGHT_FONT_COLOR_CODE.." "..barValue.." / "..barMax.." ("..barNeeded..")"..FONT_COLOR_CODE_CLOSE;
                end
				factionBar:SetMinMaxValues(0, barMax);
				factionBar:SetValue(barValue);
				color = FACTION_BAR_COLORS[standingID];
				factionBar:SetStatusBarColor(color.r, color.g, color.b);
				factionBar:SetID(factionIndex);
				factionBar:Show();
				factionHeader:Hide();
 
				-- Show a checkmark if this faction is being watched
				if ( isWatched ) then
					factionCheck:Show();
					factionName:SetWidth(100);
					factionCheck:SetPoint("LEFT", factionName, "LEFT", factionName:GetStringWidth(), 0);
				else
					factionCheck:Hide();
					factionName:SetWidth(110);
				end
 
				-- Update details if this is the selected faction
				if ( factionIndex == GetSelectedFaction() ) then
					if ( ReputationDetailFrame:IsShown() ) then
						ReputationDetailFactionName:SetText(name);
						ReputationDetailFactionDescription:SetText(description);
						if ( atWarWith ) then
							ReputationDetailAtWarCheckBox:SetChecked(1);
						else
							ReputationDetailAtWarCheckBox:SetChecked(nil);
						end
						if ( canToggleAtWar ) then
							ReputationDetailAtWarCheckBox:Enable();
							ReputationDetailAtWarCheckBoxText:SetTextColor(RED_FONT_COLOR.r, RED_FONT_COLOR.g, RED_FONT_COLOR.b);
						else
							ReputationDetailAtWarCheckBox:Disable();
							ReputationDetailAtWarCheckBoxText:SetTextColor(GRAY_FONT_COLOR.r, GRAY_FONT_COLOR.g, GRAY_FONT_COLOR.b);
 
						end
						if ( IsFactionInactive(factionIndex) ) then
							ReputationDetailInactiveCheckBox:SetChecked(1);
						else
							ReputationDetailInactiveCheckBox:SetChecked(nil);
						end
						if ( isWatched ) then
							ReputationDetailMainScreenCheckBox:SetChecked(1);
						else
							ReputationDetailMainScreenCheckBox:SetChecked(nil);
						end
					end
					getglobal("ReputationBar"..i.."Highlight1"):Show();
					getglobal("ReputationBar"..i.."Highlight2"):Show();
				else
					getglobal("ReputationBar"..i.."Highlight1"):Hide();
					getglobal("ReputationBar"..i.."Highlight2"):Hide();
				end
			end
		else
			factionHeader:Hide();
			factionBar:Hide();
		end
	end
	if ( GetSelectedFaction() == 0 ) then
		ReputationDetailFrame:Hide();
	end
end

ReputationFrame_Update = RepUpdate